<template>
  <view>
    <view class="input-search">
    	<uni-easyinput prefixIcon="search"  trim="all" v-model="keyword" :styles="styles" :placeholderStyle="placeholderStyle"></uni-easyinput>
		<button @click="search">搜索</button>
    </view>
    
    <!-- stripe type="selection" 开启多选 -->
    <uni-table ref="table" type="selection" :loading="loading" emptyText="暂无更多数据" filter-type="search"  @selection-change="selectionChange">
	  <uni-tr>
		<uni-th>
			<view class="close_btn" @click="toDelete">删除</view>
		</uni-th>
	</uni-tr>
        <uni-tr v-for="(item, index) in card_arr" :key="index" >
			<uni-swipe-action >
			<uni-swipe-action-item :right-options="right_options" show="none"
			 :auto-close="false" @click="selectes(item)">
			 <div @click="selectes(item)">
			<uni-td style="width:100vw" align="left" class="td-class">
					<view>
						<text>单号：{{ item.FBillNo }}</text>
					</view>
					<view>
						<text>日期：{{ item.FDate }}</text>
					</view>
					<view>
						<text>摘要：{{ item.FNote }}</text>
					</view>
					<view>
						<!-- <text>状态：{{ item.FStatus==1? "已完成" : "未完成"  }}</text> -->
						<text>同步状态：{{ item.FSynStatus==1? "已同步" : "未同步"  }}</text>
					</view>
			</uni-td>
			</div>
			</uni-swipe-action-item>
			</uni-swipe-action>
        </uni-tr>
    </uni-table>
	
    <view class="uni-pagination-box">
		<uni-pagination show-icon :page-size="pageSize" :current="currentPage"
        :total="total" @change="change" />
	</view>
  </view>
</template>

<script>
// 引入uni-ui组件
import {
  uniPagination,
  uniTable,
  uniTr,
  uniTh,
  uniTd,
  uniEasyinput,
  uniSwipeActionItem
} from "@dcloudio/uni-ui"
export default {
	components: {
	  uniPagination,
	  uniTable,
	  uniTr,
	  uniTh,
	  uniTd,
	  uniEasyinput,
	  uniSwipeActionItem
	},
  data() {
    return {
      card_arr: [],//当前页码的表格数据
      pageSize: 15,// 每页数据量
      currentPage: 1,// 当前页
      total: 0,// 数据总量
      loading: false,
	  placeholderStyle:'font-size:17px;',
	  styles:{
	  	//borderColor: '#999'
	  },
	  keyword:'',
	  where:'',
	  selectedIndexs:[],//选中
	  value: 1,
	  right_options: [{
			text: '明细',
			style: {
				backgroundColor: '#18bc9c'
			}
	  }]
    }
  },
  methods: {
    // 多选
	selectionChange(e) {
		this.selectedIndexs = e.detail.index
	},
	toDelete() {
		let flag = this.checkData('delete')
		if(flag){
			let _this = this;
			uni.showModal({
			    title: '确定要删除记录？',
			    success: function (res) {
			        if (res.confirm) {
						//删除本地记录
						_this.toDealData()
					}
			    }
			});
		}
	},
	checkData(){
		//验证数据
		let _this = this
		let flag = true
		if(this.selectedIndexs.length>0){
			 for(let i=0;i<this.selectedIndexs.length;i++){
				 let key = this.selectedIndexs[i]
				if(this.card_arr[key].FSynStatus==1){
					 this.$Api.msg("存在已同步记录，无法操作");
					 this.$refs.table.clearSelection()
					 return false
				}
			}
		}else{
			this.$Api.msg('请选择要操作的记录')
			return false
		}
		return flag
	},
	async toDealData(){
		//处理数据、删除本地记录
		let _this = this
		uni.showLoading({});
		await this.$Db.exeSQL('BEGIN;')
		await this.selectedIndexs.forEach(key=>{
			//删除表头
			this.$Db.deleteTableData('stockbill','FBillNo',this.card_arr[key].FBillNo).catch(err=>{
				_this.$Api.msg(err)
				uni.hideLoading();
				_this.$Db.exeSQL("ROLLBACK;")
				return
			})
			//删除表体
			this.$Db.deleteTableData('stockbillentry','FBillNo',this.card_arr[key].FBillNo).catch(err=>{
				_this.$Api.msg(err)
				uni.hideLoading();
				_this.$Db.exeSQL("ROLLBACK;")
				return
			})
		})
		uni.hideLoading()
		await this.$Db.exeSQL("COMMIT;")
		await this.$Api.msg('操作成功','success')
		await this.$refs.table.clearSelection()
		await this.search()
	},
    selectes(item) {
      if (item == '') return
	  uni.navigateTo({
	  	url:'/pages/otheroutstock/listitem',
	  		delta:1,
	  		success() {
	  			uni.$emit("getOtherOutItem", item)
	  		}
	  })
    },
	// 搜索
	search() {
		this.currentPage = 1;
		this.getlist();
	},
    //获取列表
    async getlist() {
		uni.showLoading();
		let _this = this
		if(this.keyword!=''){
			this.where = "FTranType=4 and FBillNo like '%"+this.keyword+"%' or FNote like '%"+this.keyword+"%' or FDate like '%"+this.keyword+"%'"
		}else{
			this.where = ' FTranType=4 '
		}
		await this.$Db.queryCount('stockbill',this.where).then(res => {
			this.total = res[0].num;//获得总页数
		})
		await this.$Db.queryDataList('stockbill',this.currentPage,this.pageSize,this.where,'FBillNo','desc').then(res => {
			this.card_arr = res
		}).catch(err=>{
			this.$Api.msg(err)
		})
		uni.hideLoading();
    },
    change(e) {
      this.currentPage = e.current
      this.getlist()
    },
  },
  mounted(e) {
    this.getlist()
  },
  onShow() {
  	this.getlist()
  },
  onLoad(e) {
	//this.getlist()
  }
}
</script>
<style lang="scss">

</style>